package com.muyan.design.flyweight.good;

import java.util.Map;

import com.google.common.collect.Maps;

public class FlyweightFactory {
    private static Map<String, Flyweight> flyweights = Maps.newHashMap();//

    public static Flyweight getFlyWeight(String key) {
        Flyweight flyweight = flyweights.get(key);//
        if (flyweight == null) {//
            // 浜х敓鏂扮殑ConcreteFlyweight
            synchronized (FlyweightFactory.class) {
                flyweight = flyweights.get(key);
                if (flyweight == null) {
                    flyweight = new ConcreteFlyweight(key);
                    flyweights.put(key, flyweight);//
                }
            }
        }
        return flyweight;//
    }

    public static int getFlyweightSize() {
        return flyweights.size();
    }
}
